// Package sys
// @Link  https://github.com/bufanyun/hotgo
// @Copyright  Copyright (c) 2024 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License  https://github.com/bufanyun/hotgo/blob/master/LICENSE
// @AutoGenerate Version 2.13.1
package sys

import (
	"context"
	"fmt"
	"hotgo/internal/dao"
	"hotgo/internal/library/hgorm"
	"hotgo/internal/library/hgorm/handler"
	"hotgo/internal/model/input/form"
	"hotgo/internal/model/input/sysin"
	"hotgo/internal/service"
	"hotgo/utility/convert"
	"hotgo/utility/excel"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/util/gconv"
)

type sSysStockAbnormalPool struct{}

func NewSysStockAbnormalPool() *sSysStockAbnormalPool {
	return &sSysStockAbnormalPool{}
}

func init() {
	service.RegisterSysStockAbnormalPool(NewSysStockAbnormalPool())
}

// Model 异动股票池ORM模型
func (s *sSysStockAbnormalPool) Model(ctx context.Context, option ...*handler.Option) *gdb.Model {
	return handler.Model(dao.StockAbnormalPool.Ctx(ctx), option...)
}

// List 获取异动股票池列表
func (s *sSysStockAbnormalPool) List(ctx context.Context, in *sysin.StockAbnormalPoolListInp) (list []*sysin.StockAbnormalPoolListModel, totalCount int, err error) {
	mod := s.Model(ctx)

	// 查询id
	if in.Id > 0 {
		mod = mod.Where(dao.StockAbnormalPool.Columns().Id, in.Id)
	}
	// type
	if in.Type > 0 {
		mod = mod.Where(dao.StockAbnormalPool.Columns().Type, in.Type)
	}
	// 关联表stockZhASpotEm
	mod = mod.LeftJoin(hgorm.GenJoinOnRelation(
		dao.StockAbnormalPool.Table(), dao.StockAbnormalPool.Columns().Code, // 主表表名,关联字段
		dao.StockZhASpotEm.Table(), "stockZhASpotEm", dao.StockZhASpotEm.Columns().Code, // 关联表表名,别名,关联字段
	)...)

	totalCount, err = mod.Clone().Count()
	if err != nil {
		err = gerror.Wrap(err, "获取异动股票池数据行失败，请稍后重试！")
		return
	}

	if totalCount == 0 {
		return
	}

	// 关联表select
	fields, err := hgorm.GenJoinSelect(ctx, sysin.StockAbnormalPoolListModel{}, &dao.StockAbnormalPool, []*hgorm.Join{
		{Dao: &dao.StockZhASpotEm, Alias: "stockZhASpotEm"},
	})

	if err != nil {
		err = gerror.Wrap(err, "获取异动股票池关联字段失败，请稍后重试！")
		return
	}

	if err = mod.Fields(fields).Page(in.Page, in.PerPage).OrderAsc(dao.StockAbnormalPool.Columns().Id).Scan(&list); err != nil {
		err = gerror.Wrap(err, "获取异动股票池列表失败，请稍后重试！")
		return
	}
	return
}

// Export 导出异动股票池
func (s *sSysStockAbnormalPool) Export(ctx context.Context, in *sysin.StockAbnormalPoolListInp) (err error) {
	list, totalCount, err := s.List(ctx, in)
	if err != nil {
		return
	}

	// 字段的排序是依据tags的字段顺序，如果你不想使用默认的排序方式，可以直接定义 tags = []string{"字段名称", "字段名称2", ...}
	tags, err := convert.GetEntityDescTags(sysin.StockAbnormalPoolExportModel{})
	if err != nil {
		return
	}

	var (
		fileName  = "导出异动股票池-" + gctx.CtxId(ctx) + ".xlsx"
		sheetName = fmt.Sprintf("索引条件共%v行,共%v页,当前导出是第%v页,本页共%v行", totalCount, form.CalPageCount(totalCount, in.PerPage), in.Page, len(list))
		exports   []sysin.StockAbnormalPoolExportModel
	)

	if err = gconv.Scan(list, &exports); err != nil {
		return
	}

	err = excel.ExportByStructs(ctx, tags, exports, fileName, sheetName)
	return
}

// Edit 修改/新增异动股票池
func (s *sSysStockAbnormalPool) Edit(ctx context.Context, in *sysin.StockAbnormalPoolEditInp) (err error) {
	// 修改

	var res *sysin.StockAbnormalPoolViewModel
	if err = s.Model(ctx).Where(dao.StockAbnormalPool.Columns().Type, in.Type).
		Where(dao.StockAbnormalPool.Columns().Code, in.Code).
		Where(dao.StockAbnormalPool.Columns().Date, in.Date).
		Scan(&res); err != nil {
		err = gerror.Wrap(err, "获取异动股票池信息，请稍后重试！")
		return
	} else {
		if res != nil {
			in.Id = res.Id
			if _, err = s.Model(ctx).
				Fields(sysin.StockAbnormalPoolUpdateFields{}).
				WherePri(in.Id).Data(in).Update(); err != nil {
				err = gerror.Wrap(err, "修改异动股票池失败，请稍后重试！")
			}
			return
		}

		// 新增
		if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).
			Fields(sysin.StockAbnormalPoolInsertFields{}).
			Data(in).Insert(); err != nil {
			err = gerror.Wrap(err, "新增异动股票池失败，请稍后重试！")
		}
	}
	return
}

// Delete 删除异动股票池
func (s *sSysStockAbnormalPool) Delete(ctx context.Context, in *sysin.StockAbnormalPoolDeleteInp) (err error) {
	if _, err = s.Model(ctx).Where(dao.StockAbnormalPool.Columns().Type, in.Type).
		Where(dao.StockAbnormalPool.Columns().Date, in.Date).Delete(); err != nil {
		err = gerror.Wrap(err, "删除异动股票池失败，请稍后重试！")
		return
	}
	return
}

// View 获取异动股票池指定信息
func (s *sSysStockAbnormalPool) View(ctx context.Context, in *sysin.StockAbnormalPoolViewInp) (res *sysin.StockAbnormalPoolViewModel, err error) {
	if err = s.Model(ctx).WherePri(in.Id).Scan(&res); err != nil {
		err = gerror.Wrap(err, "获取异动股票池信息，请稍后重试！")
		return
	}
	return
}
